package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 贵阳晚报
 * @author Persh
 *
 */
public class GywbNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.gywb.cn/content/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm",
		"http://comment.gywb.cn/html/[0-9\\/\\-]+/content_[0-9]+.htm"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("div.detail_title").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("h1").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.g-content-c"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".detailcon"));
		}
		String text=doc.select("#pubtime_baidu").text();
		date=DateUtils.matchDate(text);
		if(date==null) date=JSoupUtils.matchDate(doc, "时间：");
		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		GywbNewsAnalyse analyse=new GywbNewsAnalyse();
		String url="http://comment.gywb.cn/html/2015-11/27/content_4226106.htm";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
